package br.com.puertorico.mecanismos.ui.datamodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.ListDataModel;

/**
 * Class SerializableListDataModel.
 */
public class SerializableListDataModel<T extends Serializable> extends
		ListDataModel<T> implements Serializable {

	/** A constante serialVersionUID. */
	private static final long serialVersionUID = 1580231627097338715L;

	private int page;

	/** O atributo elements. */
	private List<T> elements;

	/**
	 * Construtor da classe SerializableListDataModel.
	 *
	 * @param list
	 *            - list
	 */
	public SerializableListDataModel(List<T> list) {
		super(list);
		page = 1;
		this.elements = list;
	}

	/**
	 * Get row.
	 *
	 * @return the row
	 * @see SerializableListDataModel#row.
	 */
	public T getRow() {
		return (T) super.getRowData();
	}

	/**
	 * Get elements.
	 *
	 * @return the elements
	 * @see SerializableListDataModel#elements.
	 */
	public List<T> getElements() {
		return elements;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List<T> getElementosPagina(int elementosPorPagina) {
		int offset = (page - 1) * elementosPorPagina;
		List<T> lista = new ArrayList<T>();
		for (int i = offset;; i++) {
			lista.add(getElements().get(i));
			if (lista.size() == elementosPorPagina) {
				break;
			}
		}
		return lista;
	}
}
